<?php

namespace App\Http\Controllers\AdminApi;

use App\Model\AdminTable;
use App\Model\ClassTable;
use App\Model\FieldTable;
use App\Model\LockSetTable;
use App\Model\OrderTable;
use App\Model\PriceSetTable;
use App\Model\PriceSetTempTable;
use App\Model\StorefrontTable;
use App\Model\TimeBlockTable;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Common\Fun;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;

//场地管理
class FieldController extends ApiController
{

    /**
     * @api {get} /adminapi/field/manage 场地管理
     * @apiGroup Admin-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} page --
     * @apiParam {Int} page_num --
     * @apiParam {Int} date --
     * @apiParam {Int} store_id  店铺id
     * @apiParam {Int} type  场地类型
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "count": "1",
     * "list": [
     * {
     * "id": "1",
     * "type": "1",  --球场类型
     * "storefront_id": "1", --店铺id
     * "number": "1", --场地号
     * "percent": 10 --订场率
     * }
     * ],
     * "page": 1
     * }
     * }
     */
    public function getManage()
    {
        $page = \Request::get('page', 1);
        $limit = \Request::get('page_num', 10);

        $date = \Request::get('date', date('Y-m-d'));  // 指定日期的订场率
        $store_id = \Request::get('store_id');  //店面

        $type = \Request::get('type');  //场地类型


        $param = [];
        $where = '1=1';
        if ($store_id) {
            $where .= ' and storefront_id like :storefront_id';
            $param['storefront_id'] = $store_id;
        }

        if ($type) {
            $where .= ' and type like :type';
            $param['type'] = $type;
        }


        $count = \DB::select("select count(*) as c  from field WHERE {$where}", $param)[0]->c;
        $param['offset'] = ($page - 1) * $limit;
        $param['limit'] = $limit;
        $res = \DB::select("select * from field WHERE {$where} ORDER BY id DESC limit :offset,:limit", $param);

        $list = [];
        foreach ($res as $v) {
            $v->percent = TimeBlockTable::getPercent($v->id, Str::getDayTime($date)); // 订场率获取

            $v->type_text = Str::getTypeText($v->type);
            $store_name = StorefrontTable::getStoreName($v->storefront_id);
            $list[$store_name][] = $v;
        }

        $data = [];
        foreach ($list as $k => $v) {
            $data[] = ['store_name' => $k, 'list_field' => $v];
        }

        return JsonRespose::success([
            'count' => $count,
            'list' => $data,
            'page' => $page,

        ]);

    }


    /**
     * @api {get} /adminapi/field/list 获取场地列表
     * @apiGroup Admin-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} page --
     * @apiParam {Int} page_num --
     * @apiParam {Int} store_id  店铺id 可选
     * @apiParam {Int} type  场地类型  可选
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "count": "8",
     * "list": [
     * {
     * "id": "11",
     * "type": "5",
     * "storefront_id": "1",
     * "number": "2",
     * "type_text": "5V5"
     * },
     * {
     * "id": "10",
     * "type": "5",
     * "storefront_id": "1",
     * "number": "2",
     * "type_text": "5V5"
     * },
     * {
     * "id": "6",
     * "type": "6",
     * "storefront_id": "1",
     * "number": "2",
     * "type_text": "6V6"
     * },
     * {
     * "id": "5",
     * "type": "6",
     * "storefront_id": "1",
     * "number": "2",
     * "type_text": "6V6"
     * },
     * {
     * "id": "4",
     * "type": "5",
     * "storefront_id": "1",
     * "number": "2",
     * "type_text": "5V5"
     * },
     * {
     * "id": "3",
     * "type": "5",
     * "storefront_id": "1",
     * "number": "2",
     * "type_text": "5V5"
     * },
     * {
     * "id": "2",
     * "type": "5",
     * "storefront_id": "1",
     * "number": "2",
     * "type_text": "5V5"
     * },
     * {
     * "id": "1",
     * "type": "1",
     * "storefront_id": "1",
     * "number": "1",
     * "type_text": "1V1"
     * }
     * ],
     * "page": 1
     * }
     * }
     */
    public function getList()
    {
        $page = \Request::get('page', 1);
        $limit = \Request::get('page_num', 10);
        $store_id = \Request::get('store_id');  //店面
        $type = \Request::get('type');  //场地类型
        return JsonRespose::done(FieldTable::getList($page, $limit, $store_id, $type));
    }


    /**
     * @api {get} /adminapi/field/calendar 日历浏览模式
     * @apiGroup Admin-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} month 月份
     * @apiParam {Int} id  场地id,不是编号
     * @apiParam {Int} time  时间段,取DAY_TOTAL_BLOCK_LIST第一个下标如3600一共七个
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *
     * "week": "一",  //周几
     * "day": "2018-01-01",  //日期
     * "lock": 0,  //是否锁定
     * "price": "300.00", //价格
     * "order_id": 0, 是否已预订
     * "start_time": "09:00",
     * "end_time": "11:00"
     */
    public function getCalendar()
    {


//        var_dump(TimeBlockTable::getBlockStatus(1520352000, 53,3600, 10800));die;
        //月份
        // 店面
        // 场地类型
        // 标号
        //时段

        $month = \Request::get('month', 1);  // 月份
        //$store_id = \Request::get('store_id');  //店面    前台联动
        //$type = \Request::get('type');  //场地类型    前台联动
        $id = \Request::get('id');  //编号传id
        $time = \Request::get('time');  //时段取 DAY_TOTAL_BLOCK_LIST  第一个下标

        $time = Str::timeTotime($time);
        //获取某一个月天数
        $d = cal_days_in_month(CAL_GREGORIAN, $month, date('Y'));


        $data = [];
        for ($i = 1; $i <= $d; $i++) {
            $list = [];  //时间区间和价格

            $day = strtotime(date("Y-{$month}-{$i}"));

            if ($time) {
                $times = Fun::getTimeSection($time);
                $list[] = TimeBlockTable::getBlockStatus($day, $id, $times['start_time'], $times['end_time']);
            } else {
                $time_list = TimeBlockTable::getFieldTimeList($id, $day);
                foreach ($time_list as $v) {
                    $list[] = TimeBlockTable::getBlockStatus($day, $id, $v['start_time'], $v['end_time']);

                }
            }

            //获取周几

            $week = Str::getWeek($day);

            $data[] = [
                'week' => $week,
                'day' => date('Y-m-d', $day),
                'list' => $list
            ];

        }

        return JsonRespose::success(['list' => $data]);
    }


    /**
     * @api {get} adminapi/field/info 具体场地详情
     * @apiGroup Admin-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} id 场地id
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "list": [
     * {
     * "lock": 0,   //1 已锁定 0 未锁定
     * "price": "300.00",  //价格
     * "order_id": 0, // 订单号  不为0 表示已订场
     * "start_time": "09:00",
     * "end_time": "11:00"
     * },
     * {
     * "lock": 0,
     * "price": "300.00",
     * "order_id": 0,
     * "start_time": "11:00",
     * "end_time": "13:00"
     * },
     * {
     * "lock": 0,
     * "price": "300.00",
     * "order_id": 0,
     * "start_time": "13:00",
     * "end_time": "15:00"
     * },
     * {
     * "lock": 0,
     * "price": "300.00",
     * "order_id": 0,
     * "start_time": "15:00",
     * "end_time": "17:00"
     * },
     * {
     * "lock": 0,
     * "price": "300.00",
     * "order_id": 0,
     * "start_time": "17:00",
     * "end_time": "19:00"
     * },
     * {
     * "lock": 0,
     * "price": "300.00",
     * "order_id": 0,
     * "start_time": "19:00",
     * "end_time": "21:00"
     * },
     * {
     * "lock": 0,
     * "price": "300.00",
     * "order_id": 0,
     * "start_time": "21:00",
     * "end_time": "23:00"
     * }
     * ],
     * "info": {
     * "storefront_name": "汉阳",
     * "type_text": "1V1",
     * "field_id": "1",
     * "field_type": "1",
     * "storefront_id": "1",
     * "number": "1",
     * "percent": 0 #订场率
     * }
     * }
     * }
     */
    public function getInfo()
    {
        $id = \Request::get('id');
        $time = \Request::get('time');
        //  $status = \Request::get('status');  //1已预订  2控场  3锁定

        if (!$time) {
            $day = Str::getDayTime();
        } else {
            $day = Str::getDayTime($time);
        }
        if (!$id) {
            return JsonRespose::fail();
        }

        $info = FieldTable::getFieldInfo($id);
        $list = [];
        $time_list = TimeBlockTable::getFieldTimeList($id, $day);

        foreach ($time_list as $v) {
            $data = TimeBlockTable::getBlockStatus($day, $id, $v['start_time'], $v['end_time']);
            $list[] = $data;
        }

        //status
        $info['percent'] = TimeBlockTable::getPercent($id, $day); // 订场率获取
        return JsonRespose::success(['list' => $list, 'info' => $info]);
    }

    /**
     * @api {get} adminapi/field/check 检查是否存在
     * @apiGroup Admin-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} store_id --
     * @apiParam {Int} type --
     * @apiParam {Int} number --
     *
     * @apiSuccessExample {json} Success-Response:
     *
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": 0   -- 1 表示存在
     * }
     *
     */
    public function getCheck()
    {
        $store_id = \Request::get('store_id');
        $type = \Request::get('type');
        $number = \Request::get('number');

        $res = FieldTable::ORM()->where('type', $type)->where('storefront_id', $store_id)->where('number', $number)->first();

        if ($res) {
            return JsonRespose::success(1);
        } else {
            return JsonRespose::success(0);
        }


    }

    /**
     * @api {get} adminapi/field/prices 通用价格列表
     * @apiGroup Admin-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} type 场地类型
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     * ]
     * }
     */
    public function getPrices()
    {

        $type = \Request::get('type');
        $storefront_id = \Request::get('store_id');

        $param = [];
        $where = '1=1';
        if ($type) {
            $where .= ' and price_set.type =:type';
            $param['type'] = $type;
        }
        if ($storefront_id) {
            $where .= ' and price_set.storefront_id =:storefront_id';
            $param['storefront_id'] = $storefront_id;
        }

        $res = \DB::select("select price_set.*,field.id as field_id from price_set LEFT  JOIN field ON  
(field.storefront_id=price_set.storefront_id and field.type=price_set.type and field.program=price_set.program  )   
WHERE {$where} GROUP BY price_set.id ORDER BY price_set.start_time asc ,price_set.`type` ASC   ", $param);


        $list = [];
        foreach ($res as $v) {
            $store_name = StorefrontTable::getStoreName($v->storefront_id);


            if (!$v->field_id or !$store_name) {
                continue;  //影藏没有对应场地的价格设置
            }

            $v->start_time = date('H:i', $v->start_time);
            $v->end_time = date('H:i', $v->end_time);
            $v->storefront_name = $store_name;
            $list[$store_name . '_' . $v->type . '_' . $v->program][] = $v;
        }
        $data = [];
        foreach ($list as $k => $v) {
            $data[] = [
                'storefront_name' => $v[0]->storefront_name,
                'program' => $v[0]->program,
                'program_text' => TimeBlockTable::getProgramText($v[0]->program),
                'type' => $v[0]->type,
                'type_text' => Str::getTypeText($v[0]->type),
                'list' => $v
            ];
        }


        return JsonRespose::success($data);
    }



    // 时间状态判断流程 每一条涉及到时都需要检查
    // 设置锁时需要 判断已存在的数据   对已存在的数据更新
    // 删除锁时 如果是删除单条 操作此条数据  如果是删除配置   删除此条数据,并循环查找数据库做更新操作
    // 配置临时价格时  如果是新增数据也需要反向判断锁的状态, 如果已存在则不判断


    /**
     * @api {post} adminapi/field/save 添加修改场地
     * @apiGroup Admin-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} type 场地类型,必选
     * @apiParam {Int} number 编号,必选
     * @apiParam {Int} store_id 店铺id,必选
     * @apiParam {Int} program 方案 1或者2
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     *
     * ]
     * }
     */
    public function postSave()
    {
        $type = \Request::get('type');
        $number = \Request::get('number');
        $storefront_id = \Request::get('store_id');
        $program = \Request::get('program');
        $id = \Request::get('id');

        if (!$type or !$number) {
            return JsonRespose::fail();
        }
        if (!$program) {
            return JsonRespose::fail("方案未选择");
        }

        $res = FieldTable::ORM()->where('storefront_id', $storefront_id)->where('number', $number)->first();

        if ($id) {
            //修改场地,如果方案变动, 查找最后一个订单的日期, 并且从后一天开始,更新方案为修改后的方案 时间存 第二天的0点0分0秒

            $update_data = [
                'type' => $type,
                'storefront_id' => $storefront_id,
                'number' => $number,
            ];
            $res = FieldTable::ORM()->where('id', $id)->first();
            if ($res->program != $program) {
                //查找场地下从今天开始所有订单

                $day_time = Str::getDayTime();
                $order = OrderTable::ORM()->where('field_id', $id)->where('day_time', '>=', $day_time)->where('pay_type', 2)->orderBy('order_id', 'desc')->first();

                //锁定的场次
                $lock = LockSetTable::ORM()->where('field_id', $id)->where('lock_start_time', '>=', $day_time)->orderBy('lock_start_time', 'desc')->first();

                if (!$order && !$lock) {
                    //如果没有订单,则立即生效
                    $update_data['program'] = $program;
                    $update_data['next_program'] = null;
                    $update_data['next_program_time'] = 0;
                } else {
                    $time = 0;
                    if ($order && $order->day_time > $time) {
                        $time = $order->day_time;
                    }
                    if ($lock && $lock->lock_start_time > $time) {
                        $time = $lock->lock_start_time;
                    }
                    $next_time = $time + 86400;
                    $next_time = strtotime(date('Y-m-d 00:00:00', $next_time));  //取后一天0点
                    $update_data['next_program'] = $program;
                    $update_data['next_program_time'] = $next_time;
                }
            } else {
                $update_data['program'] = $program;
                $update_data['next_program'] = null;
                $update_data['next_program_time'] = 0;
            }

            $id = FieldTable::ORM()->where('id', $id)->update($update_data);

        } else {
            if ($res) {
                return JsonRespose::fail('已存在');
            }
            $id = FieldTable::ORM()->insertGetId([
                'type' => $type,
                'storefront_id' => $storefront_id,
                'number' => $number,
                'program' => $program
            ]);

        }

        //判断场地是否有默认价格
        $res = PriceSetTable::ORM()->where('type', $type)->where('storefront_id', $storefront_id)->first();
        if (!$res) {

            // 如果不存在 是否需要自动初始化2个方案价格
            $program = 1;
            $time_list = TimeBlockTable::getTimeList($program);
            foreach ($time_list as $v) {
                PriceSetTable::ORM()->insert([
                    'start_time' => $v['start_time'],
                    'end_time' => $v['end_time'],
                    'storefront_id' => $storefront_id,
                    'type' => $type,
                    'price' => 0,//  临时价格
                    'program' => $program
                ]);
            }

            $program = 2;
            $time_list = TimeBlockTable::getTimeList($program);
            foreach ($time_list as $v) {
                PriceSetTable::ORM()->insert([
                    'start_time' => $v['start_time'],
                    'end_time' => $v['end_time'],
                    'storefront_id' => $storefront_id,
                    'type' => $type,
                    'price' => 0,//  临时价格
                    'program' => $program
                ]);
            }

            // return JsonRespose::fail('系统没有此类型的初始价格');
        }

        StorefrontTable::updateCountField($storefront_id);

        if ($id) {
            return JsonRespose::success($id);
        } else {
            return JsonRespose::fail("异常操作");
        }
    }


    /**
     * @api {post} adminapi/field/del 删除场地
     * @apiGroup Admin-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} id -
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     *
     * ]
     * }
     */
    public function postDel()
    {

        $id = \Request::get('id');

        $field = FieldTable::ORM()->where('id', $id)->first();


        FieldTable::ORM()->where('id', $id)->delete();

        $res = FieldTable::ORM()->where('type', $field->type)->where('storefront_id', $field->storefront_id)->first();


        if (!$res) {

            //反向影藏
//            PriceSetTable::ORM()->where('storefront_id', $field->storefront_id)->where('type', $field->type)->where('program',$res->program)->delete();
//            PriceSetTempTable::ORM()->where('storefront_id', $field->storefront_id)->where('type', $field->type)->where('program',$res->program)->delete();
        }

        StorefrontTable::updateCountField($res->storefront_id);

        return JsonRespose::success();
    }


    /**
     * @api {post} adminapi/field/price-save 通用价格修改
     * @apiGroup Admin-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} start_time  时间段,只需要传第一个,如9:00
     * @apiParam {Int} type 场地类型如5
     * @apiParam {Int} store_id 店铺id,必选
     * @apiParam {Int} price 新价格
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     *
     * ]
     * }
     */
    public function postPriceSave()
    {

        $start_time = \Request::get('start_time'); //只需要第一个时间
        $type = \Request::get('type');
        $storefront_id = \Request::get('store_id');
        $price = \Request::get('price');
        $program = \Request::get('program');


        if (!$start_time or !$type or !$storefront_id) {
            return JsonRespose::fail();
        }

        if (!$program) {
            return JsonRespose::fail("价格方案不存在");
        }
//        $start_time = strtotime("1970-01-01 {$start_time}:00");
        $start_time = Str::timeTotime($start_time);
//        var_dump($start_time,$type,$storefront_id,$program);die;
        $res = PriceSetTable::ORM()->where('start_time', $start_time)->where('type', $type)->where('storefront_id', $storefront_id)->where('program', $program)->update([
            'price' => $price,
        ]);


        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }

    }

    /**
     * @api {post} /adminapi/field/price-temp-save 临时价格修改
     * @apiGroup Admin-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} start_time  时间段,只需要传第一个,如9:00
     * @apiParam {Int} type 场地类型如5
     * @apiParam {Int} store_id 店铺id,必选
     * @apiParam {Int} price 新价格
     * @apiParam {Date} day 日期,如2017-01-01
     * @apiParam {Date} program --
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     *
     * ]
     * }
     */
    public function postPriceTempSave()
    {

        $start_time = \Request::get('start_time'); //只需要第一个时间
        $type = \Request::get('type');
        $storefront_id = \Request::get('store_id');
        $price = \Request::get('price');
        $day = \Request::get('day');
        $program = \Request::get('program');

        $day = strtotime($day);

        if (!$start_time or !$type or !$storefront_id or !$day) {
            return JsonRespose::fail();
        }

        if (!$program) {
            return JsonRespose::fail("方案不存在");
        }
        $start_time = Str::timeTotime($start_time);

        $price_res = PriceSetTempTable::ORM()->where('start_time', $start_time)->where('type', $type)->where('storefront_id', $storefront_id)->where('day', $day)->where('program', $program)->first();

        if ($price_res) {
            $res = PriceSetTempTable::ORM()->where('start_time', $start_time)->where('type', $type)->where('storefront_id', $storefront_id)->where('day', $day)->where('program', $program)->update([
                'price' => $price,
            ]);
        } else {
            $res = PriceSetTempTable::ORM()->insertGetId([
                'price' => $price,
                'start_time' => $start_time,
                'end_time' => Fun::getTimeSection($start_time)['end_time'],
                'type' => $type,
                'storefront_id' => $storefront_id,
                'day' => $day,
                'program' => $program
            ]);
        }


        if ($res) {
            return JsonRespose::success();
        } else {
            return JsonRespose::fail();
        }

    }


    /**
     * @api {get} /adminapi/field/price-temp-list 临时价格列表
     * @apiGroup Admin-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} type 场地类型如5
     * @apiParam {Int} store_id 店铺id
     * @apiParam {Date} day 日期,如2017-01-01
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     * {
     * "storefront_name": "汉阳",
     * "list": [
     * {
     * "id": "36",
     * "start_time": "09:00",
     * "end_time": "11:00",
     * "price": "300.00",
     * "storefront_id": "1",
     * "type": "1"
     * },
     * {
     * "id": "37",
     * "start_time": "11:00",
     * "end_time": "13:00",
     * "price": "300.00",
     * "storefront_id": "1",
     * "type": "1"
     * },
     * {
     * "id": "38",
     * "start_time": "13:00",
     * "end_time": "15:00",
     * "price": "300.00",
     * "storefront_id": "1",
     * "type": "1"
     * },
     * {
     * "id": "39",
     * "start_time": "15:00",
     * "end_time": "17:00",
     * "price": "300.00",
     * "storefront_id": "1",
     * "type": "1"
     * },
     * {
     * "id": "40",
     * "start_time": "17:00",
     * "end_time": "19:00",
     * "price": "300.00",
     * "storefront_id": "1",
     * "type": "1"
     * },
     * {
     * "id": "41",
     * "start_time": "19:00",
     * "end_time": "21:00",
     * "price": "300.00",
     * "storefront_id": "1",
     * "type": "1"
     * },
     * {
     * "id": "42",
     * "start_time": "21:00",
     * "end_time": "23:00",
     * "price": "300.00",
     * "storefront_id": "1",
     * "type": "1"
     * }
     * ]
     * },
     * {
     * "storefront_name": "武昌",
     * "list": []
     * },
     * {
     * "storefront_name": "234234324",
     * "list": []
     * }
     * ]
     * }
     */
    public function getPriceTempList()
    {


        $type = \Request::get('type');
        $day = \Request::get('day');
        $storefront_id = \Request::get('store_id');
        $day = strtotime($day);

        if (!$day) {
            return JsonRespose::fail();
        }

        $param = [];
        $where = '1=1';
        if ($type) {
            $where .= ' and price_set.type =:type';
            $param['type'] = $type;
        }
        if ($storefront_id) {
            $where .= ' and price_set.storefront_id =:storefront_id';
            $param['storefront_id'] = $storefront_id;
        }

        $res = \DB::select("select price_set.*,field.id as field_id from price_set LEFT  JOIN field ON  
(field.storefront_id=price_set.storefront_id and field.type=price_set.type and field.program=price_set.program  )   
WHERE {$where} GROUP BY price_set.id ORDER BY price_set.start_time asc ,price_set.`type` ASC   ", $param);


        $list = [];
        foreach ($res as $v) {

            $store_name = StorefrontTable::getStoreName($v->storefront_id);


            if (!$v->field_id or !$store_name) {
                continue;  //影藏没有对应场地的价格设置
            }

            $price_res = PriceSetTempTable::ORM()->where('start_time', $v->start_time)->where('end_time', $v->end_time)->where('type', $v->type)->where('storefront_id', $v->storefront_id)
                ->where('day', $day)->where('program', $v->program)->first();
            if ($price_res) {
                $v->price = $price_res->price;
            }

            $v->start_time = date('H:i', $v->start_time);
            $v->end_time = date('H:i', $v->end_time);
            $v->storefront_name = $store_name;
            $list[$store_name . '_' . $v->type . '_' . $v->program][] = $v;
        }
        $data = [];
        foreach ($list as $k => $v) {
            $data[] = [
                'storefront_name' => $v[0]->storefront_name,
                'program' => $v[0]->program,
                'program_text' => TimeBlockTable::getProgramText($v[0]->program),
                'type' => $v[0]->type,
                'type_text' => Str::getTypeText($v[0]->type),
                'list' => $v
            ];
        }


        return JsonRespose::success($data);

    }


    /**
     * @api {post} /adminapi/field/lock 锁定操作
     * @apiGroup Admin-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} field_id 场地
     * @apiParam {Time} start_time 时间起始区间,如9:00
     * @apiParam {Date} rep_model 1 每周 2每月 3 某一天 （原理是复用每周的逻辑,只是限定范围在某一天）
     * @apiParam {Date} set_value 设置的值  1~31(没有前缀0)  或 1~7
     * @apiParam {Date} lock_start_time 锁定开始时间,如2017-01-01
     * @apiParam {Date} lock_end_time 锁定结束时间,如2017-01-01
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     * ]
     * }
     */
    public function postLock()
    {

        $field_id = \Request::get('field_id');
        $start_time = \Request::get('start_time'); //需要锁定的时间段
        $rep_model = \Request::get('rep_model'); //'1 每周 2每月',
        $set_value = \Request::get('set_value'); //'设置的值  1~31(没有前缀0)  或 1~7',
        $lock_start_time = \Request::get('lock_start_time'); //锁定开始时间
        $lock_end_time = \Request::get('lock_end_time');  //锁定结束时间


        if (!$rep_model or !$start_time or !$field_id) {
            return JsonRespose::fail();
        }

        if ($rep_model == 3) {  //转变成周配置
            $rep_model = 1;
            $set_value = date('N', strtotime($lock_start_time));;// 取周几
        }
        $start_time = Str::timeTotime($start_time);

        //锁定的时候不能确定到某一天,所以..
        $res = LockSetTable::ORM()->insertGetId([
            'field_id' => $field_id,
            'start_time' => $start_time,
            'end_time' => Fun::getTimeSection($start_time)['end_time'],
            'lock_start_time' => strtotime($lock_start_time),
            'lock_end_time' => strtotime($lock_end_time),
            'rep_model' => $rep_model,
            'set_value' => $set_value
        ]);


        if ($res) {
            return JsonRespose::success($res);
        }

    }


    /**
     * @api {post} /adminapi/field/un-lock 解锁
     * @apiGroup Admin-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Date} day 日期,如2017-01-01
     * @apiParam {Int} field_id 场地id
     * @apiParam {Date} start_time 要操作的时间段,如9:00
     * @apiParam {Date} action  1这一次2所有
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     * ]
     * }
     */
    public function postUnLock()
    {

        $day = \Request::get('day');
        $field_id = \Request::get('field_id');
        $start_time = \Request::get('start_time'); //需要锁定的时间段
        $action = \Request::get('action'); //1 这一次 2 所有


        if (!$field_id or !$start_time or !$action) {
            return JsonRespose::fail();
        }


        $day = strtotime($day);
        $start_time = Str::timeTotime($start_time);


        if ($action == 1) {

            LockSetTable::ORM()->where('field_id', $field_id)->where('start_time', $start_time)->where('lock_start_time', $day)->delete();  //删除


            $block = TimeBlockTable::ORM()->where('field_id', $field_id)->where('day', $day)->where('start_time', $start_time)->first();
            if ($block) {
                $res = TimeBlockTable::ORM()->where('field_id', $field_id)->where('day', $day)->where('start_time', $start_time)->update([
                    'lock' => 0
                ]);
            } else {
                $res = TimeBlockTable::ORM()->where('field_id', $field_id)->where('day', $day)->where('start_time', $start_time)->insertGetId([
                    'lock' => 0,
                    'field_id' => $field_id,
                    'day' => $day,
                    'start_time' => $start_time,
                    'end_time' => Fun::getTimeSection($start_time)['end_time'],
                ]);
            }
        }

        if ($action == 2) {

            return JsonRespose::fail('禁用');
            $res = LockSetTable::ORM()->delete();
            TimeBlockTable::ORM()->where('day', '>=', time())->update([
                'lock' => 0
            ]);


        }


        return JsonRespose::success($res);

    }

    public function getTimeList()
    {

        $field_id = \Request::get('field_id');
        $field_res = FieldTable::ORM()->where('id', $field_id)->first();

        $time_list = TimeBlockTable::getTimeList($field_res->program);


        $data = [];
        foreach ($time_list as $v) {
            $v['start_time'] = date('H:i', $v['start_time']);
            $v['end_time'] = date('H:i', $v['end_time']);
            $data[] = $v;
        }
        return JsonRespose::success($data);
    }


}